/**
 * Created by ZhouWei on 2017/6/17.
 */



$(function () {
    windowChange();
    $(window).resize(function() {
        windowChange();
    });

    var phoneNumber,verificationCode,registerPwd,repeatPwd = false;

    $("input[name='phoneNumber']").blur(function(){
        var value = $(this).val();
        if(value){
            if((/^1[34578]\d{9}$/.test(value))){
                $('#getVer').removeAttr("disabled");
                $(this).parent().find("p").html('手机号可用').css("color","green").fadeIn();
                var _this = this;
                setTimeout(function(){
                    $(_this).parent().find("p").fadeOut();
                },2000);
                phoneNumber = true;
            }else {
                phoneNumber = false;
                $(this).parent().find("p").html('手机号码有误，请重新输入').css("color","red").fadeIn();
            }
        }else{
            $(this).parent().find("p").html('手机号码不能位空，请重新输入').css("color","red").fadeIn();
        }
        windowChange();
    });
    $("input[name='verificationCode']").blur(function(){
        if(this.value){
            if((/^[0-9]{4,6}$/.test(this.value))){
                verificationCode = true;
                $(this).parent().find("p").html('格式正确').css("color","green").fadeIn();
                var _this = this;
                setTimeout(function(){
                    $(_this).parent().find("p").fadeOut();
                },2000);
            }else{
                verificationCode = false;
                $(this).parent().find("p").html('验证码格式有误，请重新输入').css("color","red").fadeIn();
            }
        }else{
            $(this).parent().find("p").html('验证码不能为空，请重新输入').css("color","red").fadeIn();
        }
        windowChange();
    });
    $("input[name='registerPwd']").blur(function(){
        if(this.value){
            if(this.value.length >= 6){
                registerPwd = true;
                $("input[name='repeatPwd']").removeAttr("disabled");
                $(this).parent().find("p").html('密码可用').css("color","green").fadeIn();
                var _this = this;
                setTimeout(function(){
                    $(_this).parent().find("p").fadeOut();
                },2000);
            }else{
                registerPwd = false;
                $("input[name='repeatPwd']").attr("disabled","disabled");
                $(this).parent().find("p").html('密码最低位6位，请重新输入').css("color","red").fadeIn();
            }
        }else{
            $(this).parent().find("p").html('密码不能为空，请重新输入').css("color","red").fadeIn();
        }
        windowChange();
    });
    $("input[name='repeatPwd']").blur(function(){
        var prePwd = $("input[name='registerPwd']").val();
        if(this.value.length >= 6 && this.value == prePwd){
            repeatPwd = true;
            $(this).parent().find("p").html('输入正确').css("color","green").fadeIn();
            var _this = this;
            setTimeout(function(){
                $(_this).parent().find("p").fadeOut();
            },2000);
        }else{
            repeatPwd = false;
            $(this).parent().find("p").html('两次输入密码不一致').css("color","red").fadeIn();
        }
        windowChange();
    });
    $('#getVer').click(function(){
        //ajax()

        //验证码接口
        var username=$("input[name='phoneNumber']").val();

        $.ajax({

            type:"POST",
            url:"http://zcai.cn/requestSmsCode",
            // dataType: 'jsonp',//从php返回的值以 JSON方式 解释
            // jsonp:"callback",//
            // jsonpCallback:"login",//
            dataType:"JSON",
            data:{
                username:username
            },
            success:function (text) {

            }

        });


        //60秒倒计时
        var time = 60;
        var timer = setInterval(function(){
            if(time < 0){
                $('#getVer').html("获取验证码");
                $('#getVer').removeAttr("disabled");
                clearInterval(timer);
            }else{
                $('#getVer').attr("disabled","disabled");
                $('#getVer').html(time+'s后重新发送');
                time--;
            }
        },1000);
    });

    $('form').submit(function() {
        var data = $(this).serialize();
        if(phoneNumber && verificationCode && registerPwd && repeatPwd){
            $("#screen").fadeIn(100);
            //ajax
            var username=$("input[name='phoneNumber']").val();
            //registerPwd
            var password=$("input[name='registerPwd']").val();

            var verifyCode=$("input[name='verificationCode']").val();

            //修改密码接口
            $.ajax({

                type:"POST",
                url:"http://zcai.cn/forgetPwd",
                // dataType: 'jsonp',//从php返回的值以 JSON方式 解释
                // jsonp:"callback",//
                // jsonpCallback:"login",//
                dataType:"JSON",
                data:{
                    username:username,
                    password:password,
                    verifyCode:verifyCode
                },
                success:function (text) {
                    $("#screen").fadeOut(100);
                    localStorage.setItem("token",text.data.token);
                    localStorage.setItem("username",text.data.手机);
                    window.location.href='home1.html';
                }

            });

        }else{
            $('.report').fadeIn(1000).html("请填写正确的注册信息");
            setTimeout(function(){
                $('.report').fadeOut(1000);
            },3000)

        }
        return false;
    });
});



function windowChange(){
    if($(window).height()>1080){
        $('.form-group').css("height","100px").addClass("form-group-lg");
        $('.formButton').addClass("btn-lg");
        var wH = $(window).height();
        var mH = parseInt( $('.main').css('height'));
        var mT = (wH-mH*2)/2 + "px";
        $('.main').css('marginTop',mT);
        $("body").css("zoom",1.5)
    }else if($(window).height()<800){
        $("body").css("zoom",1);
        $('.formButton').addClass("btn-lg");
        var wH = $(window).height();
        var mH = parseInt( $('.main').css('height'));
        var mT = (wH-mH)/2 + "px";
        $('.main').css('marginTop',mT);
    }else{
        $("body").css("zoom",1);
        $('.form-group').css("height","59px").removeClass("form-group-lg");
        $('.main').css('marginTop',"150px");
        $('.formButton').removeClass("btn-lg");
    }
}















